package com.hd123.rumba.gwt.util.client;

import java.util.Date;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Window;

/**
 * 性能分析日志
 * 
 * @author LiQi
 * 
 */
public class PerformanceLogger {

  public static Date startTime;

  private static String log = "";

  private static DateTimeFormat timeFormat = DateTimeFormat.getFormat("HH:mm:ss.SSS");

  public static void append(String s, Date startTime) {
    log += timeFormat.format(startTime) + "~" + timeFormat.format(new Date()) + " [" + s + "] "
        + timeTaken(startTime) + "\n";
  }

  public static void showLog() {
    Window.alert(log);
    clearLog();
  }

  public static void clearLog() {
    log = "";
  }

  public static String getLog() {
    return log;
  }

  private static String timeTaken(Date startTime) {
    Date endTime = new Date();
    double totalSeconds = (endTime.getTime() - startTime.getTime()) / 1000.0;

    return NumberFormat.getFormat("0.000").format(totalSeconds) + "秒";
  }
}
